//对axios的统一封装
import axios from 'axios';
import qs from 'qs';
import {Message} from 'element-ui'

//统一的跨域前缀
axios.defaults.baseURL = 'http://localhost:8086/';
axios.defaults.timeout = 5000;

//拦截器配置
axios.interceptors.request.use((config) => {
  if(config.method === 'post') {
     //将参数格式利用qs进行序列化转化
    config.data = qs.stringify(config.data,{arrayFormat: 'repeat'});
  }else if(config.method==='put'){
    config.data = qs.stringify(config.data,{arrayFormat: 'repeat'});
// 		let ret = '',data = config.data;
//     for (let it in data) {
//         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
//     }
//     config.data=ret.endsWith("&")?ret.substr(0,ret.length-1):ret;
		config.headers={
			'Content-Type': 'application/x-www-form-urlencoded'
		}
}
  return config;
}, (error) => {
		return Message.error({message: '请求超时!'});
});
export default axios; 